package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Systemmenu;
import cn.wolfcode.wms.qo.MenuQueryObject;
import cn.wolfcode.wms.service.ISystemmenuService;
import cn.wolfcode.wms.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("systemmenu")
public class SystemmenuController {
    @Autowired
    private ISystemmenuService systemmenuService;
    @ResponseBody
    @RequestMapping("getMenusBySn")
    public Object getMenusBySn(String sn){
        //{id:11,pid:1,name:"部门管理",action:"..."}
        //通过sn查询系统菜单  用么用对象封装,要么直接用map封装,列名为key,值为value
       List<Map<String,Object>> menus = systemmenuService.getMenusBySn(sn);
        JSONResult json = new JSONResult();
        return menus;
    }


    @RequestMapping("list")
    public String list(Model model,@ModelAttribute("qo") MenuQueryObject qo){
        //面包屑 通过id查找所有的父菜单爷爷菜单
        List<Systemmenu> menus = systemmenuService.selectParentMenusByParentId(qo.getParentId());

        model.addAttribute("menus",menus);
        List<Systemmenu> list = systemmenuService.selectall(qo);

        model.addAttribute("result",list);
        return "systemmenu/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id,Long parentId){
        if (id != null) {
            Systemmenu entity = systemmenuService.get(id);
            model.addAttribute("entity",entity);
        }
        //通过父id查询父菜单
        Systemmenu parent = systemmenuService.selectByParentId(parentId);
            model.addAttribute("parent",parent);
        return "systemmenu/input";
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Systemmenu entity){
       systemmenuService.saveOrUpdate(entity);
        return new JSONResult();
    }


}
